<?php

require_once WEB_ROOT . '/protected/extensions/qrcode/phpqrcode.php';
class formatResult
{
    public $merchant_id = '';
    public $term_id = '';
    public $timestamp = '';
    public $biz_channel = '';
    public $biz_type = '';
    public $code = '';
    public $message = '';
    public $biz_content = array();
    public $sign_type = '';

    public function __construct($data)
    {
        foreach($data as $k => $v){
            if(isset($this->$k) && $k != 'biz_content'){
                $this->$k = $v;
            }
        }
        $this->sign_type = 'MD5';
        $this->timestamp = time();
    }

    public function getData()
    {
        $ret = get_object_vars($this);
        if(isset($ret['code'])){
            $ret['code'] = $ret['code'] . '';
        }
        foreach($ret['biz_content'] as $k => $v){
            if(is_string($v) || is_numeric($v) || is_float($v) || is_double($v)){
                $ret['biz_content'][$k] = $v . '';
            }
        }

        if(empty($ret['biz_content'])){
            $ret['biz_content'] = new stdClass();
        }

        return $ret;
    }
}


function lgrouter($info, $time = null, $filename = 'routerg')
{
    $time = empty($time)? time() : $time;

    $filename = app()->params['logs_root']['system_worker'] . $filename . '.' . date('Ymd', $time);
    $handler = fopen($filename, 'a+');

    $info = (is_array($info) || is_object($info))? json_encode($info, JSON_UNESCAPED_UNICODE) : $info;
    fputs($handler, date('Y-m-d H:i:s', $time) . ': ' . $info . "\n\n");
    fclose($handler);
}

class ConsoleCommand extends CConsoleCommand
{

    protected $_worker;

    public function __construct($name, $runner)
    {
        return parent::__construct($name, $runner);
    }

    public function beforeRun(&$args)
    {
        $this->_worker= new GearmanWorker();
        $gearmanIp = '127.0.0.1';
        $gearmanPort = '4730';

        exec("ip addr |grep global|awk '{print \$2}'|awk -F\/ '{print \$1}'", $out, $ret);
        $inetIp = empty($out[0])? '' : $out[0];
        if(empty($inetIp)){
            echo "启动失败\n";
            echo "获取inet ip失败\n";
            exit();
        }

        echo "本机INETIP: {$inetIp}\n";
        $gearmanConfig = app()->db->createCommand("
			SELECT * FROM GEARMAN_CONFIG 
		    WHERE INETIP = '".$inetIp."' 
		")->queryRow();

        if(!empty($gearmanConfig)){
            $gearmanIp = $gearmanConfig['WORKERS_IP'];
            $gearmanPort = $gearmanConfig['WORKERS_PORT'];
        }else{
            echo "启动失败\n";
            echo "获取WORKERS_IP失败\n";
            exit();
        }

        echo "Gearman Workers工作组IP: {$gearmanIp}\n";
        echo "Gearman Workers工作组端口{$gearmanPort}\n";
        $this->_worker->addServer($gearmanIp, $gearmanPort);

        return true;
    }

    protected function _signReturn($format, $data, $token, $job = null, $signCal = '')
    {
        $sign = '';
        if($format == 'md5'){
            $sign = SignMd5Helper::genSign($data, $token);
        }

        $response = array(
            'data' => $data,
            'sign' => $sign
        );

        if($job){
            if(empty($signCal)){
                lg(array(
                    'workloaded' => $job->workload(),
                    'response' => $response,
                ));
            }else{
                lg(array(
                    'workloaded' => $job->workload(),
                    'response' => $response,
                    'signCal' => $signCal
                ));
            }
        }

        return json_encode($response, JSON_UNESCAPED_UNICODE);
    }

    protected function _signNotiMch($format, $data, $token)
    {
        $sign = '';
        if($format == 'md5'){
            $sign = SignMd5Helper::genNotiMchSign($data, $token);
        }

        $response = array(
            'data' => $data,
            'sign' => $sign
        );

        return json_encode($response);
    }

    protected function _signRefundNotiMch($format, $data, $token)
    {
        $sign = '';
        if($format == 'md5'){
            $sign = SignMd5Helper::genRefundNotiMchSign($data, $token);
        }

        $response = array(
            'data' => $data,
            'sign' => $sign
        );

        return json_encode($response);
    }

}